package io.github.junxworks.qt.modules.mm.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.auth.model.UserModel;
import io.github.junxworks.ep.core.exception.BusinessException;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.constants.FieldStatus;
import io.github.junxworks.qt.modules.mm.dto.FieldDto;
import io.github.junxworks.qt.modules.mm.entity.MmField;
import io.github.junxworks.qt.modules.mm.mapper.FieldMapper;
import io.github.junxworks.qt.modules.mm.service.FieldService;
import io.github.junxworks.qt.modules.mm.vo.FieldVo;

@Service
public class FieldServiceImpl implements FieldService {

	@Autowired
	private FieldMapper fieldMapper;

	@Override
	public PageInfo<FieldVo> queryFieldList(FieldDto dto) {
		PageUtils.setPage(dto);
		return new PageInfo<FieldVo>(fieldMapper.queryFieldList(dto));
	}

	@Override
	public int addField(FieldDto dto) {
		MmField field = new MmField();
		BeanUtils.copyProperties(dto, field);
		UserModel user = (UserModel) SecurityUtils.getSubject().getPrincipal();
		field.setCreateDate(new Date());
		field.setCreatorId(user.getId());
		field.setStatus(FieldStatus.NORMAL.getValue());
		return fieldMapper.insertWithoutNull(field);
	}

	/* (non-Javadoc)
	 * @see io.github.junxworks.qt.modules.mm.service.FieldService#eidtAndBackupField(io.github.junxworks.qt.modules.mm.dto.FieldDto)
	 */
	@Override
	@Transactional
	public int eidtAndBackupField(FieldDto dto) {
		UserModel user = (UserModel) SecurityUtils.getSubject().getPrincipal();
		Long userId = user.getId();
		MmField mf = fieldMapper.selectEntityByID(MmField.class, dto.getId());
		if (!userId.equals(mf.getCreatorId())) {
			throw new BusinessException("不是创建者不能操作数据");
		}
		MmField nfield = new MmField();
		BeanUtils.copyProperties(dto, nfield);
		nfield.setModifyDate(new Date());
		nfield.setModifierId(userId);
		return fieldMapper.updateWithoutNull(nfield);
	}

	@Override
	public FieldVo queryFieldDetailById(Long id) {
		return fieldMapper.getOneByKey(id);
	}

	@Override
	public int deleteField(Long id) {
		UserModel user = (UserModel) SecurityUtils.getSubject().getPrincipal();
		Long userId = user.getId();
		MmField mf = fieldMapper.selectEntityByID(MmField.class, id);
		if (!userId.equals(mf.getCreatorId())) {
			throw new BusinessException("不是创建者不能操作数据");
		}
		List<String> dsNames = fieldMapper.queryDsNameByFieldId(id);
		if (dsNames.isEmpty()) {
			return fieldMapper.deleteField(id);
		} else {
			throw new BusinessException("字段有关联数据流:" + StringUtils.join(",", dsNames.toArray(new String[0])));
		}
	}

	@Override
	public List<FieldVo> queryFieldDetailByIds(Long[] ids) {
		return fieldMapper.queryFieldDetailByIds(ids);
	}

	@Override
	public FieldVo queryFieldDetailByCName(String cName) {
		return fieldMapper.queryFieldDetailByCName(cName);
	}
}
